﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Universe.SimpleQueue.Tests
{
    partial class TypicalWorkers
    {

        static Random Rand = new Random(1);
        private static readonly string SameMessageId = "{4104624D-A2DD-43E0-8717-DB9E8D859160}";

        static public Action<SimpleQueue> PublishMessage(string queueName, int minSize, int maxSize)
        {
            return queue =>
            {
                int size;
                lock (Rand)
                    size = Rand.Next(minSize, maxSize);

                queue.Publish(queueName, new byte[size]);
            };
        }

        static public Action<SimpleQueue> PublishSameMessage(string queueName, int messageSize)
        {
            return queue =>
            {
                queue.Publish(queueName, SameMessageId, new byte[messageSize]);
            };
        }

        static public Action<SimpleQueue> PublishSameMessage_WithCheck(string queueName, int messageSize)
        {
            return queue =>
            {
                if (queue.GetMessageStatus(SameMessageId) == null)
                    queue.Publish(queueName, SameMessageId, new byte[messageSize]);
            };
        }


        static public Action<SimpleQueue> PublishNewMessage(string queueName, int minSize, int maxSize)
        {
            return queue =>
            {
                int size;
                lock (Rand)
                    size = Rand.Next(minSize, maxSize);
                
                queue.Publish(queueName, Guid.NewGuid().ToString(), new byte[size]);
            };
        }

        static public Action<SimpleQueue> PublishNewMessage_WithCheck(string queueName, int minSize, int maxSize)
        {
            return queue =>
            {
                int size;
                lock (Rand)
                    size = Rand.Next(minSize, maxSize);

                var optionalKey = Guid.NewGuid().ToString();
                if (queue.GetMessageStatus(optionalKey) == null)
                {
                    queue.Publish(queueName, optionalKey, new byte[size]);
                }
            };
        }

    }
}
